<?php

namespace App\Jobs;

use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use GuzzleHttp\Client;
use App\Models\SmsSendError;

class SyncSendSms implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $action;
    public $mobiles;
    public $content;
    public $otherInfos;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($action, $mobiles, $content, $otherInfos = [])
    {
        $this->action  = $action;
        $this->mobiles = $mobiles;
        $this->content = $content;
        $this->otherInfos = $otherInfos;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        try{
            Log::info($this->action ."\t". $this->mobiles ."\t". $this->content);
            $resp = $this->sendSmsByEmay($this->mobiles, $this->content);
            Log::info(json_encode($resp));
        }catch(Exception $e) {
            Log::error($e->getMessage());
        }
    }

    public function sendSmsByEmay($mobiles, $content)
    {
        $client = new Client();
        $now = date('Ymdhis');
        if ($this->action == "phone") {
            $appId =  config('app.sms.emay_app_id2');
            $secretKey = config('app.sms.emay_secret_key2');
        } else if ($this->action == "card"){
            $appId =  config('app.sms.emay_app_id1');
            $secretKey = config('app.sms.emay_secret_key1');
        } else if ($this->action == "pingan"){
            $appId =  config('app.sms.emay_app_id3');
            $secretKey = config('app.sms.emay_secret_key3');
        } else {
            throw new Exception('action不存在');
        }
        // Log::info($appId ."\t". $secretKey);
        $data = [
            'appId' => $appId,
            'timestamp' => $now,
            'sign' => md5($appId . $secretKey . $now),
            'mobiles' => $mobiles,
            'content' => $content
        ];
        $fullUrl = 'http://shmtn.b2m.cn/simpleinter/sendSMS';
        $response = $client->request('GET', $fullUrl, [
            'query' => $data,
            'timeout' => 5
        ]);
        $resp_status = $response->getStatusCode();
        if ($resp_status == 200) {
            $resp_content = $response->getBody()->getContents();
            Log::info($resp_content);
            $respObj = json_decode($resp_content, true);
            if (json_last_error() != JSON_ERROR_NONE || !isset($respObj['code']) || $respObj['code'] != 'SUCCESS') {
                SmsSendError::create([
                    'mobile' => $mobiles,
                    'action' => $this->action,
                    'resp_content' => $resp_content,
                    'other_infos' => json_encode($this->otherInfos)
                ]);
                throw new Exception('shmtn.b2m.cn 接口返回json格式错误');
            }

            return $respObj;
        } else {
            SmsSendError::create([
                'mobile' => $mobiles,
                'action' => $this->action,
                'resp_content' => 'shmtn.b2m.cn 接口错误',
                'other_infos' => json_encode($this->otherInfos)
            ]);
            throw new Exception('shmtn.b2m.cn 接口错误');
        }
    }
}
